<template>
	<view>
		<view class="my-settle-container">
			<!-- 全选区域 -->
			<label class="radio" @click="changeAllState">
				<radio color="#c00000" :checked="isFullCheck" /><text>全选</text>
			</label>
			<!-- 合计区域 -->
			<view class="amount-box">
				合计:<text class="amount">￥{{checkedGoodsAmount}}</text>
			</view>
			<!-- 计算按钮 -->
			<view class="btn-settle" @click="settlement">
				结算({{checkedCount}})
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState, mapGetters, mapMutations } from "vuex"
	export default {
		data() {
			return {
				seconds: 3,
				timer: null
			};
		},
		computed: {
			...mapGetters('m_cart', ['checkedCount', 'total', 'checkedGoodsAmount']),
			...mapGetters('m_user', ['addstr']),
			...mapState('m_user', ['token']),
			isFullCheck() {
				return this.total === this.checkedCount
			}
		},
		methods: {
			...mapMutations('m_cart', ['updateAllGoodsState', 'updateRedirectInfo']),
			changeAllState() {
				console.log(!this.isFullCheck)
				this.updateAllGoodsState(!this.isFullCheck)
			},
			settlement() {
				if (!this.checkedCount) return uni.$showMgs('清选择要结算的商品')
				if (!this.addstr) return uni.$showMgs('请选择收货地址')
				// if (!this.token) return uni.$showMgs('请先登录')
				if (!this.token) return this.delayNavigate()

				this.payOrder()
			},
			delayNavigate() {
				this.seconds = 3
				this.showTips(this.seconds)
				this.timer = setInterval(() => {
					this.seconds--
					if (this.seconds <= 0) {
						clearInterval(this.timer)
						uni.switchTab({
							url: '/pages/my/my',
							success: () => {
								this.updateRedirectInfo({
									openType: 'switchTab',
									from: '/pages/cart/cart'
								})
							}
						})
						return
					}
					this.showTips(this.seconds)
				}, 1000)
			},
			showTips(n) {
				uni.showToast({
					icon: 'none',
					title: '请登陆后再结算' + n + '秒后自动跳转到登录页',
					mask: true,
					duration: 1500
				})
			},
			async payOrder() {
				const orderInfo = {
					order_price: 0.01,
					cconsignee_addr: this.addstr,
					goods: this.cart.filter(x => x.goods_state).map(x => ({
						goods_id: x.goods_id,
						goods_number: x.goods_count,
						goods_price: x.goods_price
					}))
				}
				// 发起请求创建订单
				const { data: result } = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
				if (result.meta.status !== 200) return uni.$showMgs('创建订单失败')
				// 得到服务器相应的订单编号
				const orderNumber = result.message.order_number
				// 获取订单的支付信息
				const { data: result2 } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', { order_number: orderNumber })
				// 预付订单生成失败
				if (result2.meta.status !== 200) return uni.$showMgs('预支付订单生成失败')
				// 得到支付订单相关的必要参数
				const payInfo = result2.pay

				// 发起支付
				const [err, succ] = await uni.requestPayment(payInfo)
				// 支付未完成
				if (err) return uni.$showMgs('订单未支付')
				const { data: result3 } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', { order_number: orderNumber })
				if (result3.meta.status !== 20) return uni.$showMgs('订单未支付')
				uni.showToast({
					title: '支付完成',
					icon: 'success'
				})

			}
		}
	}
</script>

<style lang="scss">
	.my-settle-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 50px;
		background-color: white;

		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-left: 5px;
		font-size: 14px;

		.radio {
			display: flex;
			align-items: center;
		}

		.amount {
			color: #C00000
		}

		.btn-settle {
			height: 50px;
			min-width: 100px;
			background-color: #C00000;
			color: white;
			line-height: 50px;
			text-align: center;
			padding: 0 10px;
		}
	}
</style>
